<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/13/013
 * Time: 11:36
 */

namespace app\api\service;

class Express {

    /**
     * 订阅物流信息
     * @param $order
     * @param $express_sn
     * @return bool
     */
    public static function subscriptionExpress($order, $express_sn) {
        // 订阅物流信息
        // 获取快递单号所属公司
        $express_sn_for_company = get_data('http://www.kuaidi100.com/autonumber/auto?num=' . $express_sn . '&key=PqvxmYAa8643');
        $comCode = $express_sn_for_company[0]['comCode'];
        $poll_param = [
            'company' => $comCode,
            'number' => $express_sn,
            'to' => $order->province . $order->city . $order->area,
            'key' => 'PqvxmYAa8643',
            'parameters' => [
                'callbackurl' => 'http://e.mgtvshop.com/api/callback/express',
                'salt' => $order->order_sn . '&' . $order->id,
            ]
        ];
        $poll_data = [
            'schema' => 'json',
            'param' => json_encode($poll_param)
        ];
        $o="";
        foreach ($poll_data as $k=>$v) {
            $o.= "$k=".urlencode($v)."&";		//默认UTF-8编码格式
        }

        $poll_data=substr($o,0,-1);

        $url = 'http://poll.kuaidi100.com/poll';
        $callback_data = post_data($url, $poll_data);
        if ($callback_data['returnCode'] == 200) {
            $order->save(['express_subscription' => 1], ['id' => $order->id]);
        }

        return true;
    }
}